//
//  SSLoginViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/10/10.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSLoginViewController.h"
#import "SSUserInfoManager.h" 


@interface SSLoginViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginButtonHeight;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;


@end

@implementation SSLoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.userNameTF.text.length) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.passwordTF becomeFirstResponder];
        });
    }else{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.userNameTF becomeFirstResponder];
        });
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"登录";
    self.loginButtonHeight.constant = kRealValue(45);
    [self setLeftNav];
}

- (void)setLeftNav {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back)];

}

- (void)back {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)loginButtonClick:(UIButton *)sender {
     [self.view endEditing:YES];
    if (self.userNameTF.text.length == 0) {
        [self showTip:@"账号不能为空"];
        return;
    }
    if (self.passwordTF.text.length == 0) {
        [self showTip:@"请输入密码"];
        return;
    }
    
    [self showHUD];
    NSDictionary *param = @{@"loginName":self.userNameTF.text,@"password":self.passwordTF.text};
    
    [SSNetworkManager getWithURLString:kSSUserInfoAPI parameters:param success:^(NSDictionary *responseDict) {
        [self dismissHUD];
        NSInteger isLogin = [responseDict[@"data"][@"isLogin"] integerValue];
        NSString *tip = responseDict[@"data"][@"tip"];
        if (isLogin == 0) {
            [self showTip:tip];
        }else{
            [SSHud showTip:tip InView:[UIApplication sharedApplication].keyWindow];
             [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessKey object:nil];
            [[SSUserInfoManager sharedManager] didLoginInWithUserInfo:responseDict[@"data"]];
           
          
            [self back];
        
        }
        
    } failure:^(NSError *error) {
        
    }];
    
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
